package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.consts.Const;
import com.example.demo.dto.PoetryListDTO;
import com.example.demo.entity.Author;
import com.example.demo.entity.Poetry;
import com.example.demo.mapper.AuthorMapper;
import com.example.demo.mapper.PoetryMapper;
import com.example.demo.service.AuthorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AuthorServiceImpl implements AuthorService {

    @Resource
    private AuthorMapper authorMapper;

    @Resource
    private PoetryMapper poetryMapper;

    @Override
    public List<String> getInitAuthorInfo() {
        return authorMapper.getInitAuthorInfo();
    }

    @Override
    public Author getRandomAuthor() {
        return authorMapper.getRandomAuthor();
    }

    @Override
    public PoetryListDTO getPoetryListDTO(Integer authorId, Integer pageNo) {
        Author author = authorMapper.selectById(authorId);
        //分页查询 该作者的古诗词
        QueryWrapper<Poetry> wrapper = new QueryWrapper();
        wrapper.eq("author_id",authorId);
        Page<Poetry> page = new Page<>(pageNo, Const.PAGE_SIZE);
        IPage<Poetry> userIPage = poetryMapper.selectPage(page, wrapper);
        List<Poetry> records = userIPage.getRecords();
        return new PoetryListDTO(author, records);
    }
}
